package textArea;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import document.AwesomeDocument;

/**
 * The amazing panel that encapsulates the text pane and other awesome things.
 * 
 */
public class AwesomeTextPanel extends JPanel {
	
	private JScrollPane scrollPane;
	private JTextPane textPane;
	
	/**
	 * The flag set when the document has been changed in any way.
	 */
	private boolean changed;
	
	/**
	 * The document type associated with the currently opened document.
	 */
	private AwesomeDocument doc;
	
	public AwesomeTextPanel() {
		this("");
		
		
	}
	
	public AwesomeTextPanel(String text) {
		super(new BorderLayout());
		this.initComponents();
		textPane.setText(text);
		changed = false;
	}

	private void initComponents() {
		this.textPane = new AwesomeTextPane();
		textPane.setFont(new Font("Monospaced", 0, 12));
		this.scrollPane = new JScrollPane(textPane);
		this.add(scrollPane);
		doc = null;
		textPane.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void changedUpdate(DocumentEvent arg0) {
			}

			@Override
			public void insertUpdate(DocumentEvent arg0) {
				changed = true;
				
			}

			@Override
			public void removeUpdate(DocumentEvent arg0) {
				changed = false;
				
			}
			
		});
	}
	public void setDocumentType(AwesomeDocument doc) {
		this.doc = doc;
	}
	
	public AwesomeDocument getDocumentType() {
		return this.doc;
	}
	
	public JTextPane getTextPane() {
		return this.textPane;
	}
	
	public boolean isChanged() {
		return this.changed;	
	}
	
	public void setChanged(boolean flag) {
		this.changed = flag;
	}
}
